package com.jiangjiang.eshop.purchase.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.purchase.convert.SupplierConvert;
import com.jiangjiang.eshop.purchase.dao.SupplierMapper;
import com.jiangjiang.eshop.purchase.domain.SupplierEntity;
import com.jiangjiang.eshop.purchase.dto.SupplierDTO;
import com.jiangjiang.eshop.purchase.dto.SupplierQueryDTO;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;

/**
 * @Author Jianghaowen
 * @Date 2021/9/14
 * @Description: TODO
 * @Version V1.0
 **/
@Service
public class SupplierService extends ServiceImpl<SupplierMapper, SupplierEntity> {

    @Autowired
    private SupplierConvert supplierConvert;

    /**
     * 分页查询供应商数据
     * @param dto 查询条件
     * @return 查询结果
     */
    public Page<SupplierDTO> queryForPage(SupplierQueryDTO dto) {
        Page<SupplierEntity> page = new Page<>(dto.getPageNo(), dto.getPageSize(), true);
        LambdaQueryWrapper<SupplierEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(Strings.isNotEmpty(dto.getName()), SupplierEntity::getName, dto.getName())
                .eq(dto.getSettlementPeriod() != null, SupplierEntity::getSettlementPeriod, dto.getSettlementPeriod());
        return supplierConvert.convertPage2DTO(this.getBaseMapper().selectPage(page, wrapper));
    }


    /**
     * 根据id查询供应商数据
     * @param id 供应商id
     * @return 查询结果
     */
    public SupplierDTO queryById(Long id){
        return supplierConvert.convert2DTO(this.getById(id));
    }

    /**
     * 保存供应商数据
     * @param dto  入参
     * @retur 处理结果
     */
    public Boolean save(SupplierDTO dto) {
        return this.save(supplierConvert.convert2Entity(dto));
    }


    /**
     * 根据采购结算周期查询供应商数据
     * @param settlementPeriod 采购结算周期
     * @return 查询结果
     */
    public List<SupplierDTO> queryBySettlementPeriod(Integer settlementPeriod){
        LambdaQueryWrapper<SupplierEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SupplierEntity::getSettlementPeriod, settlementPeriod);
        return supplierConvert.convert2DTOList(this.list(wrapper));
    }

}
